package com.dyz.swordFingerOffer;

import java.util.ArrayList;

/**
 * @author: daiyizheng
 * @date: 2021/6/10 21:31
 * @description:
 */
public class FindContinuousSequence57 {
    public static void main(String[] args) {
//         ArrayList<ArrayList<Integer>> arrayLists = new FindContinuousSequence57().FindContinuousSequence(9);
//        System.out.println(arrayLists);
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.remove(2);//remove的是索引

    }
    public ArrayList<ArrayList<Integer>> FindContinuousSequence(int sum) {
        ArrayList<ArrayList<Integer>> ans = new ArrayList<ArrayList<Integer>>();
        int l = 1;//左指针起始位置
        int r = 2;//右指针起始位置
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        while(l<(sum+1)/2){
            int tempSum = cul(l,r);
            while(tempSum>sum){
                tempSum -=l;
                l++;
                list.remove(0);
            }
            if(tempSum == sum){
                ans.add(new ArrayList<>(list));
                l++;
                list.remove(0);
            }
            r++;
            list.add(r);
        }
        return ans;
    }

    private int cul(int l, int r) {
        return (l+r)*(r-l+1)/2;
    }
}
